//
//  HWPayBookView.m
//  嗨文
//
//  Created by 响当当 on 5/17/19.
//  Copyright © 2019 dongzheng. All rights reserved.
//

#import "HWPayBookView.h"
#import "HWBookReadViewController.h"
#import "HWReChargeViewController.h"
#import "HWRechargeVIPViewController.h"

@interface HWPayBookView()
@property (nonatomic, strong)UIView *footView;
@property (nonatomic, strong)UILabel *bookCoinLabel;
@property (nonatomic, strong)UILabel *accountMoneyLabel;
@property (nonatomic, strong)UIButton *buyBtn;
@property (nonatomic, strong)HWBookReaderModel *model;
@end

@implementation HWPayBookView

- (UIView *)footView
{
    if (!_footView)
    {
        _footView = [[UIView alloc]initWithFrame:CGRectMake(0,SCREEN_HEIGHT/2,SCREEN_WIDTH,SCREEN_HEIGHT/2)];
        _footView.backgroundColor = [UIColor whiteColor];
    }
    return _footView;
}

- (UILabel *)bookCoinLabel
{
    if (!_bookCoinLabel)
    {
        _bookCoinLabel = [HWUtilties creatLabelWithFrame:CGRectMake(15*kUIScaleBasePlus,10*kUIScaleBasePlus,SCREEN_WIDTH-30*kUIScaleBasePlus,20*kUIScaleBasePlus)
                                            textAligment:NSTextAlignmentLeft
                                                textClor:[UIColor blackColor]
                                                textFont:15*kUIScaleBasePlus];
    }
    return _bookCoinLabel;
}

- (UILabel *)accountMoneyLabel
{
    if (!_accountMoneyLabel)
    {
        _accountMoneyLabel = [HWUtilties creatLabelWithFrame:CGRectMake(15*kUIScaleBasePlus,CGRectGetMaxY(self.bookCoinLabel.frame)+10*kUIScaleBasePlus,SCREEN_WIDTH-30*kUIScaleBasePlus,20*kUIScaleBasePlus)
                                                textAligment:NSTextAlignmentLeft
                                                    textClor:[UIColor blackColor]
                                                    textFont:15*kUIScaleBasePlus];
    }
    return _accountMoneyLabel;
}

- (UIButton *)buyBtn
{
    if (!_buyBtn)
    {
        _buyBtn = [HWUtilties creatButtonWithFrame:CGRectMake(15*kUIScaleBasePlus,CGRectGetMaxY(self.accountMoneyLabel.frame)+20*kUIScaleBasePlus,SCREEN_WIDTH-30*kUIScaleBasePlus,48*kUIScaleBasePlus)
                                      textAligment:NSTextAlignmentCenter
                                              text:@"充值"
                                          textClor:LCHexColor(0xffffff)
                                         buttonBGC:JSAMainColor
                                          textFont:15*kUIScaleBasePlus
                                            target:self
                                            method:@selector(rechanrge)];
        _buyBtn.layer.masksToBounds = YES;
        _buyBtn.layer.cornerRadius = _buyBtn.frame.size.height/2;
    }
    return _buyBtn;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
     
        self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.01];
        
        [self addSubview:self.footView];
        [self.footView addSubview:self.bookCoinLabel];
        [self.footView addSubview:self.accountMoneyLabel];
        [self.footView addSubview:self.buyBtn];
        UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(10*kUIScaleBasePlus,0,SCREEN_WIDTH-20*kUIScaleBasePlus,1*kUIScaleBasePlus)];
        lineView.backgroundColor = JSALineColor;
        [self.footView addSubview:lineView];
    }
    return self;
}

- (void)getInfo:(HWBookReaderModel *)model
{
    self.model = model;
    NSString *chapterCoinStr = [NSString stringWithFormat:@"价格：%@书券",[model.chapterOrder stringValue]];
    self.bookCoinLabel.text = chapterCoinStr;
    
    NSString *accountStr = [NSString stringWithFormat:@"余额：%@书币+%@书券",[model.bookCoin stringValue],[model.giftCoin stringValue]];
    self.accountMoneyLabel.text = accountStr;
  
    NSInteger account = [model.bookCoin integerValue]  + [model.giftCoin integerValue];

    
    if (account > [model.chapterCoin integerValue])
    {
        if ([[model.autoCutPay stringValue] isEqualToString:@"0"] )
        {
            [self.buyBtn setTitle:@"兑换" forState:UIControlStateNormal];
        }
    }
    else
    {
        [self.buyBtn setTitle:@"充值" forState:UIControlStateNormal];
    }
}

//充值
- (void)rechanrge
{
    if ([self.buyBtn.titleLabel.text isEqualToString:@"兑换"])
    {
        NSDictionary *prarmers = @{
                                   @"autoCutPay" : @"1",
                                   @"access_token" : current_User.token
                                   };
        WeakSelf(self)
        [NetWorkingManager postRequestUrl:FULL_WEB_URL(kAPI_PAYBOOK) withPramater:prarmers sucess:^(id responseObject) {
            if (weakSelf.paySuccessBlock)
            {
                weakSelf.paySuccessBlock();
            }
        } failure:^(NSError *error) {
            
        }];
    }
    else
    {
        
        if (current_User.isHideinfo.integerValue == 0 )
        {
            HWReChargeViewController *vc = [[HWReChargeViewController alloc]init];
            vc.hidesBottomBarWhenPushed = YES;
            
            dispatch_async(dispatch_get_main_queue(), ^{
                [[HWUtilties getCurrentVC].navigationController pushViewController:vc animated:YES];
                //            [[weakSelf viewController].navigationController pushViewController:vc animated:YES];
            });
        }
        else
        {
            HWRechargeVIPViewController *vc = [[HWRechargeVIPViewController alloc]init];
            vc.hidesBottomBarWhenPushed = YES;
            
            dispatch_async(dispatch_get_main_queue(), ^{
                [[HWUtilties getCurrentVC].navigationController pushViewController:vc animated:YES];
                //            [[weakSelf viewController].navigationController pushViewController:vc animated:YES];
            });
        }
    
     
    }
    
}

- (UIViewController *)viewController
{
    UIViewController *viewController = nil;
    UIResponder *next = self.nextResponder;
    while (next) {
        if ([next isKindOfClass:[HWBookReadViewController class]]) {
            viewController = (HWBookReadViewController *)next;
            break;
        }
        next = next.nextResponder;
    }
    return viewController;
}

@end
